package com.aaa.statistics.service.impl;


import com.aaa.entity.CheckItem;
import com.aaa.entity.DictData;
import com.aaa.entity.HisCheckResult;
import com.aaa.statistics.dao.CheckDao;
import com.aaa.statistics.feign.CheckItemFeign;
import com.aaa.statistics.feign.DictDataFeign;
import com.aaa.statistics.service.CheckService;
import com.aaa.statistics.vo.HisCheckResultSaleVo;
import com.aaa.statistics.vo.HisCheckResultVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * @author ：晋升（js20020923@qq.com）
 * @date ：Created in 2023/3/29 9:49
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class CheckServiceImpl extends ServiceImpl<CheckDao,HisCheckResult> implements CheckService {
    @Autowired
    private CheckDao checkDao;
    @Autowired
    private CheckItemFeign checkItemFeign;
    @Autowired
    private DictDataFeign dictDataFeign;

    /**
     * 初始化检查项目
     * @return
     */
    @Override
    public Result<List<CheckItem>> queryList() {
        List<CheckItem> all = checkItemFeign.queryList();
        return new Result<>(2000,"查询所有检查项目成功",all);
    }

    /**
     * 通过字典类型查询对应的检查状态
     * @param type
     * @return
     */
    @Override
    public Result<List<DictData>> fingBytype(String type) {
        List<DictData> dictData = dictDataFeign.listType(type);
        return new Result<>(2000,"查询检查状态字典信息成功",dictData);
    }

    @Override
    public Result<IPage<HisCheckResult>> HisetByPage(Integer currentPage, Integer pageSize, HisCheckResultVo hisCheckResultVo) {
        IPage<HisCheckResult> page=new Page<>(currentPage,pageSize);
        QueryWrapper<HisCheckResult> wrapper=new QueryWrapper<>();
        //条件查询  前端传递数据到后端
        if (Objects.nonNull(hisCheckResultVo.getCheckItemId())){
            //字段名要跟数据库内保持一致
            wrapper.eq("check_item_id",hisCheckResultVo.getCheckItemId());
        }
        if (StringUtils.hasText(hisCheckResultVo.getPatientName())){
            //字段名要跟数据库内保持一致
            wrapper.like("patient_name",hisCheckResultVo.getPatientName());
        }
        if (Objects.nonNull(hisCheckResultVo.getDateRange())&&hisCheckResultVo.getDateRange().length==2){
            wrapper.between("create_time",hisCheckResultVo.getDateRange()[0],hisCheckResultVo.getDateRange()[1]);
        }
        else {
            LocalDateTime now = LocalDateTime.now();
            String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            wrapper.like("create_time",format);//默认当前时间
        }
        IPage<HisCheckResult> page1 = checkDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询药品销售",page1);
    }

    @Override
    public Result<List<HisCheckResultSaleVo>> listStat(HisCheckResultVo hisCheckResultVo) {
        List<HisCheckResultSaleVo> hisCareOrderItemVos = checkDao.selectquery(hisCheckResultVo);
        return new Result<>(2000,"查询药品销售",hisCareOrderItemVos);
    }
}

